/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;
import java.util.*;

/*******************************************************************************
 * 
 * This is a wrapper class for JDBC data sources. Currently supports Oracle,
 * PostgreSQL and MySQL
 * 
 * @author
 * 
 * This code is the property of Los Alamos National Laboratory and the
 * University of California, Copyright 2005
 */

public class JDBCDatabase {

    /***************************************************************************
     * Host name of the database server (should be renamed in a future release)
     */
    private String url;

    /***************************************************************************
     * User name for the database server
     */
    private String user = "";

    /***************************************************************************
     * Password for the database server
     */
    private String password = "";

    private Properties props = null;

    /***************************************************************************
     * User name for the database server
     */
    protected Connection connection;

    /***************************************************************************
     * 
     * This constructor instantiates a MySQL database connection
     * 
     * @param dbURL
     * @param user
     * @param password
     * @throws SQLException
     * @throws ClassNotFoundException
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    public JDBCDatabase(String dbURL, String user, String password) throws SQLException, ClassNotFoundException,
            IllegalAccessException, InstantiationException {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        connection = DriverManager.getConnection(dbURL, user, password);
        this.url = dbURL;
        this.user = user;
        this.password = password;
    }

    public JDBCDatabase(String dbURL, Properties props) throws SQLException, ClassNotFoundException,
            IllegalAccessException, InstantiationException {
        Class.forName("org.postgresql.Driver").newInstance();
        connection = DriverManager.getConnection(dbURL, props);
        this.url = dbURL;
        this.props = props;
    }

    /***************************************************************************
     * 
     * Default contructor
     * 
     */
    public JDBCDatabase() {

    }

    /***************************************************************************
     * 
     * Generates a Statement associated with the already open database
     * connection
     * 
     * Implement Exception handling for Oracle and PostgreSQL
     * 
     * @return A Statement object generated by the Connection
     * 
     */
    public Statement createStatement() {
        Statement s = null;
        try {
            s = connection.createStatement();
            // statements.addElement(s);
        } catch (Exception E) {
            E.printStackTrace();
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                if (props == null)
                    connection = DriverManager.getConnection(url, user, password);
                else
                    connection = DriverManager.getConnection(url, props);
                s = connection.createStatement();
            } catch (Exception E2) {
                E2.printStackTrace();
            }

        }
        return s;
    }

    /***************************************************************************
     * 
     * This method should be called when finished with the database connection.
     * Failure to do so could result in connections to the server remaining
     * open.
     * 
     */
    public void closeConnection() {
        try {

            if (!connection.isClosed())
                connection.close();
        } catch (Exception E) {
            // E.printStackTrace();
        }
    }

    /***************************************************************************
     * 
     * Call this method to get the Connection object that was opened when this
     * object was instantiated.
     * 
     * @return Returns a pointer to the database connection that was opened
     *         during instantiation of the Database object.
     */
    public Connection getConnection() {
        return connection;
    }

}